<template>
	<view class="body">
		<input v-model="email" 
		class="uni-input common-input" 
		placeholder="输入要绑定的邮箱" />
		
		<input v-model="password" 
		class="uni-input common-input" 
		placeholder="输入密码" 
		password />
		
		<button 
		class="user-set-btn" 
		type="primary"
		:class="{'user-set-btn-disable' : disable}" 
		:disabled="disable"  
		:loading="loading"
		@tap="submit"
		>完成</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: '',
				password: '',
				disable: true,
				loading: false
			}
		},
		watch:{
			email(val){
				this.change()
			},
			password(val){
				this.change()
			}
		},
		methods: {
			// 监听输入改变
			change(){
				if(this.email && this.password){
					this.disable = false;
					return;
				}
				this.disable = true;
			},
			// 验证
			check(){
				if(!this.email || this.email.trim() === ''){
					uni.showToast({
						title: '邮箱不能为空',
						icon:'none'
					})
					return false;
				}
				
				// 验证邮箱格式
				let ePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(!ePattern.test(this.email)){
					uni.showToast({
						title: '请输入正确邮箱',
						icon:'none'
					})
					return false;
				}
				
				if(!this.password || this.password.trim() === ''){
					uni.showToast({
						title: '密码不能为空',
						icon:'none'
					})
					return false;
				}
				
				return true;
			},
			// 提交
			submit(){
				if(!this.check()){
					return;
				}
				this.loading = true;
				this.disable = true;
				setTimeout(() => {
					this.disable = false;
					this.loading = false;
				},2000);
				
			}	
		}
	}
</script>

<style>
	@import '../../common/form.css';
</style>
